package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.domian.ShoppingCart;
import com.itheima.mapper.client.ShoppingCartMapper;
import com.itheima.service.client.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 12441
 * @Date 2022/8/18 14:52
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    @PostMapping("/add")
    public R<ShoppingCart>addShoppingCart(@RequestBody ShoppingCart shoppingCart){

        log.info("对象数据：{}", shoppingCart);
        shoppingCart.setUserId(TokenHolder.getCurrentId());

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,TokenHolder.getCurrentId());

        //判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){

            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询数据
        ShoppingCart selectOne = shoppingCartMapper.selectOne(queryWrapper);
        //判断菜品是否已经存在，存在则amount+1
        if(selectOne!=null){
            //菜品/套餐 存在，执行更新操作
            Integer number = selectOne.getNumber();
            selectOne.setNumber(number+1);
            shoppingCartMapper.updateById(selectOne);
        }
        else {
            //菜品/套餐 不存在，执行插入操作
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }


        return R.success(shoppingCart,"添加成功");
    }
    @GetMapping("/list")
    public R<List<ShoppingCart>>showList(@RequestHeader("AuthorizationUser") String token){


        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,TokenHolder.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);

        return R.success(shoppingCarts,"查询成功");
    }
    @DeleteMapping("/clean")
    public R<String>deleteShoppingCart(){
        //获取当前用户的ID
        String currentId = TokenHolder.getCurrentId();
        log.info("当前的用户ID为：{}",currentId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartMapper.delete(queryWrapper);

        return R.success("清空成功");
    }
    @PostMapping("/sub")
    public R<String>subDishFromShoppingCart(@RequestBody ShoppingCart shoppingCart){

        shoppingCart.setUserId(TokenHolder.getCurrentId());

        Long dishId = shoppingCart.getDishId();
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>" + dishId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,TokenHolder.getCurrentId());
        if (dishId!=null) {

            queryWrapper.eq(ShoppingCart::getDishId, dishId);
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);

            Integer number = one.getNumber();

            if (number >0) {
                number -= 1;
                one.setNumber(number);
                shoppingCartService.updateById(one);
                if (number==0){
                    shoppingCartMapper.delete(queryWrapper);
                    return R.success("减少成功");
                }
                return R.success("减少成功");
            } else {
                shoppingCartMapper.delete(queryWrapper);
                return R.success("减少成功");
            }
        }
            Long setmealId = shoppingCart.getSetmealId();

            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart one2 = shoppingCartService.getOne(queryWrapper);

            Integer number2 = one2.getNumber();

            if (number2>0){
                number2-=1;
                one2.setNumber(number2);
                shoppingCartService.updateById(one2);
                if (number2==0){
                    shoppingCartMapper.delete(queryWrapper);
                }
            }
            else {
                shoppingCartMapper.delete(queryWrapper);
            }

        return R.success("减少成功");
    }
}